<template>
  <div class="test">
    <!-- <button @click="list.push(Date.now())">add</button>

    <transition-group
      name="fade"
      appear
    >
      <li
        v-for="item in list"
        :key="item"
      >
        {{ item }}
      </li>
    </transition-group> -->

    <div
      id="list-demo"
      class="demo"
    >
      <button v-on:click="add">Add</button>
      <button v-on:click="remove">Remove</button>
      <transition-group
        name="list"
        tag="p"
      >
        <span
          v-for="item in items"
          v-bind:key="item"
          class="list-item"
        >
          {{ item }}
        </span>
      </transition-group>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      items: [1, 2, 3, 4, 5, 6, 7, 8, 9],
      nextNum: 10
    }
  },
  methods: {
    randomIndex: function () {
      return Math.floor(Math.random() * this.items.length)
    },
    add: function () {
      this.items.splice(this.randomIndex(), 0, this.nextNum++)
    },
    remove: function () {
      this.items.splice(this.randomIndex(), 1)
    }
  }
}
</script>
<style lang="scss" scoped>
// .fade-enter,
// .fade-leave-to {
//   transform: translateX(200px);
//   opacity: 0;
// }

// // 设置进入和离开动画的时间，可以分开写
// .fade-enter-active,
// .fade-leave-active {
//   transition: all 0.8s ease;
// }

.list-item {
  display: inline-block;
  margin-right: 10px;
}
.list-enter-active, .list-leave-active {
  transition: all 1s;
}
.list-enter, .list-leave-to
/* .list-leave-active for below version 2.1.8 */ {
  opacity: 0;
  transform: translateY(30px);
}
</style>
